package com.ruoyi.customer.controller;

import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.customer.domain.CustomerGroup;
import com.ruoyi.customer.service.ICustomerGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
public class CustomerGroupController extends BaseController {
    @Autowired
    private ICustomerGroupService groupService;

    /**
     * 查询顾客分组列表
     */
    @RequiresPermissions("group:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerGroup group) {
        startPage();
        List<CustomerGroup> list = groupService.selectCustomerGroupList(group);
        return getDataTable(list);
    }

    /**
     * 获取顾客分组详细信息
     */
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
        return AjaxResult.success(groupService.selectCustomerGroupById(groupId));
    }

    /**
     * 新增顾客分组
     */
    @PostMapping
    public AjaxResult add(@RequestBody CustomerGroup group) {
        if (groupService.insertCustomerGroup(group) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error(HttpStatus.ERROR, "新增顾客分组失败");
    }

    /**
     * 修改顾客分组
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerGroup group) {
        if (groupService.updateCustomerGroup(group) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error(HttpStatus.ERROR, "修改顾客分组失败");
    }

    /**
     * 删除顾客分组
     */
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        return toAjax(groupService.deleteCustomerGroupByIds(groupIds));
    }
}
